package com.sttk.secreconeclick.bookkeep.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sttk.secreconeclick.bookkeep.BaseAc;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.adapter.SelIconAdapter;
import com.sttk.secreconeclick.bookkeep.adapter.SelTypeAdapter;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookBean;
import com.sttk.secreconeclick.bookkeep.bean.BillTypeBean;
import com.sttk.secreconeclick.bookkeep.databinding.ActivityAddTypeBinding;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;

public class AddTypeActivity extends BaseAc<ActivityAddTypeBinding> implements View.OnClickListener, OnItemClickListener {

    public static int accountPos;
    public static int selPosition;
    public static boolean isIncome;
    private SelIconAdapter selIconAdapter;
    private BillTypeBean billTypeBean;
    List<AccountBookBean> listSp;
    AccountBookBean accountBookBean;

    @Override
    protected int onCreate() {
        return R.layout.activity_add_type;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAddTypeBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);

        mDataBinding.rvIconList.setLayoutManager(new GridLayoutManager(mContext, 4));
        selIconAdapter = new SelIconAdapter();
        mDataBinding.rvIconList.setAdapter(selIconAdapter);
        selIconAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvSave) {//保存
            String name = mDataBinding.etName.getText().toString();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort(getString(R.string.please_input_name));
                return;
            }

            billTypeBean.setName(name);

            if (selPosition == -1) {
                if (isIncome) {
                    if (accountBookBean.getIncomeTypeList() == null) {
                        accountBookBean.setIncomeTypeList(new ArrayList<>());
                    }
                    accountBookBean.getIncomeTypeList().add(billTypeBean);
                } else {
                    if (accountBookBean.getExpenditureTypeList() == null) {
                        accountBookBean.setExpenditureTypeList(new ArrayList<>());
                    }
                    accountBookBean.getExpenditureTypeList().add(billTypeBean);
                }
            }

            AppSpUtils.saveAccountBookList(listSp);
            ToastUtils.showShort(getString(R.string.add_suc));
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        billTypeBean.setIcon(selIconAdapter.getItem(position).getIcon());
        billTypeBean.setSelIcon(selIconAdapter.getItem(position).getSelIcon());
        billTypeBean.setSaveIcon(selIconAdapter.getItem(position).getSaveIcon());
        mDataBinding.ivImg.setImageResource(selIconAdapter.getItem(position).getIcon());
    }

    @Override
    protected void initData() {
        List<BillTypeBean> listShow = new ArrayList<>();
        listShow.add(new BillTypeBean(R.drawable.gongzi2, R.drawable.gongzi4, R.drawable.gongzi3, "工资"));
        listShow.add(new BillTypeBean(R.drawable.canyin2, R.drawable.canyin4, R.drawable.canyin3, "餐饮"));
        listShow.add(new BillTypeBean(R.drawable.shuiguo2, R.drawable.shuiguo4, R.drawable.shuiguo3, "水果"));
        listShow.add(new BillTypeBean(R.drawable.maicai2, R.drawable.maicai4, R.drawable.maicai3, "买菜"));
        listShow.add(new BillTypeBean(R.drawable.haizi2, R.drawable.haizi4, R.drawable.haizi3, "孩子"));
        listShow.add(new BillTypeBean(R.drawable.lvxing2, R.drawable.lvxing4, R.drawable.lvxing3, "旅行"));
        listShow.add(new BillTypeBean(R.drawable.riyongpin2, R.drawable.riyongpin4, R.drawable.riyongpin3, "日用品"));
        listShow.add(new BillTypeBean(R.drawable.jiudian2, R.drawable.gongzi4, R.drawable.gongzi3, "酒店"));
        listShow.add(new BillTypeBean(R.drawable.yanjiu2, R.drawable.yanjiu4, R.drawable.yanjiu3, "烟酒"));
        listShow.add(new BillTypeBean(R.drawable.fushi2, R.drawable.fushi4, R.drawable.fushi3, "服饰"));
        listShow.add(new BillTypeBean(R.drawable.yiyao2, R.drawable.yiyao4, R.drawable.yiyao3, "医药"));
        listShow.add(new BillTypeBean(R.drawable.baoxian2, R.drawable.baoxian4, R.drawable.baoxian3, "保险"));
        listShow.add(new BillTypeBean(R.drawable.xuexi2, R.drawable.xuexi4, R.drawable.xuexi3, "学习"));
        listShow.add(new BillTypeBean(R.drawable.dianqi2, R.drawable.dianqi4, R.drawable.dianqi3, "电器"));
        listShow.add(new BillTypeBean(R.drawable.shuma2, R.drawable.shuma4, R.drawable.shuma3, "数码"));
        listShow.add(new BillTypeBean(R.drawable.touzi2, R.drawable.touzi4, R.drawable.touzi3, "投资"));
        listShow.add(new BillTypeBean(R.drawable.jiekuan2, R.drawable.jiekuan4, R.drawable.jiekuan3, "借款"));
        listShow.add(new BillTypeBean(R.drawable.jiangjin2, R.drawable.jiangjin4, R.drawable.jiangjin3, "奖金"));
        listShow.add(new BillTypeBean(R.drawable.hongbao2, R.drawable.hongbao4, R.drawable.hongbao3, "红包"));
        listShow.add(new BillTypeBean(R.drawable.tuikuan2, R.drawable.tuikuan4, R.drawable.tuikuan3, "退款"));
        listShow.add(new BillTypeBean(R.drawable.baoxiao2, R.drawable.baoxiao4, R.drawable.baoxiao3, "报销"));
        selIconAdapter.setList(listShow);

        listSp = AppSpUtils.getAccountBookList();
        accountBookBean = listSp.get(accountPos);

        if (selPosition == -1) {
            billTypeBean = new BillTypeBean(R.drawable.gongzi2, R.drawable.gongzi4, R.drawable.gongzi3, "");
        } else {
            if (isIncome) {
                billTypeBean = accountBookBean.getIncomeTypeList().get(selPosition);
            } else {
                billTypeBean = accountBookBean.getExpenditureTypeList().get(selPosition);
            }
            mDataBinding.etName.setText(billTypeBean.getName());
        }

        mDataBinding.ivImg.setImageResource(billTypeBean.getIcon());
    }
}